package com.com.zs21cp.wordcountDemo;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;


public class flink_wordcount_Stream_01 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> file = env.socketTextStream("hadoop2", 10086);

        SingleOutputStreamOperator<Tuple2<String, Long>> splitWord = file.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Long>> out) throws Exception {
                String[] split = s.split(" ");
                for (String word : split) {
                    out.collect(Tuple2.of(word, 1L));
                }
            }
        });
        KeyedStream<Tuple2<String, Long>, String> keyBy =
                splitWord.keyBy(data -> data.f0);
        keyBy.sum(1).print();

        env.execute();
    }


}


